package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.ExamUserMapper;
import com.youlai.boot.system.service.ExamUserService;
import com.youlai.boot.system.model.entity.ExamUser;
import com.youlai.boot.system.model.form.ExamUserForm;
import com.youlai.boot.system.model.query.ExamUserQuery;
import com.youlai.boot.system.model.vo.ExamUserVO;
import com.youlai.boot.system.converter.ExamUserConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 考生服务实现类
 *
 * @author youlaitech
 * @since 2025-07-15 15:15
 */
@Service
@RequiredArgsConstructor
public class ExamUserServiceImpl extends ServiceImpl<ExamUserMapper, ExamUser> implements ExamUserService {

    private final ExamUserConverter examUserConverter;

    /**
    * 获取考生分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<ExamUserVO>} 考生分页列表
    */
    @Override
    public IPage<ExamUserVO> getExamUserPage(ExamUserQuery queryParams) {
        Page<ExamUserVO> pageVO = this.baseMapper.getExamUserPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取考生表单数据
     *
     * @param id 考生ID
     * @return 考生表单数据
     */
    @Override
    public ExamUserForm getExamUserFormData(Long id) {
        ExamUser entity = this.getById(id);
        return examUserConverter.toForm(entity);
    }
    
    /**
     * 新增考生
     *
     * @param formData 考生表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveExamUser(ExamUserForm formData) {
        ExamUser entity = examUserConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新考生
     *
     * @param id   考生ID
     * @param formData 考生表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateExamUser(Long id,ExamUserForm formData) {
        ExamUser entity = examUserConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除考生
     *
     * @param ids 考生ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteExamUsers(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的考生数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
